//
//  CategoryItem.m
//  kankanVideo
//
//  Created by wangbo on 6/1/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "CategoryItem.h"
#import "MyRequest.h"
#import "ASIHTTPRequest.h"
#import "XMLParser.h"
#import "MyCoreData.h"
#import "IndexData.h"

@implementation CategoryItem
@synthesize lblTitle;
@synthesize status;
@synthesize btn;
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame withTitle:(NSString *)theTitle  withURL:(NSString*)theURL withIndex:(NSNumber *)theIndex
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    index=theIndex;
    url=theURL;
    [self setBackgroundColor:[UIColor whiteColor]];
    btn=[UIButton buttonWithType:UIButtonTypeCustom];
    //if ([url isEqualToString:@""]) {
    //}else{
        [btn addTarget:self action:@selector(btnTapped:) forControlEvents:UIControlEventTouchUpInside];
    //}
    //[btn setFrame:frame];
    [btn setFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
    [btn setImage:[UIImage imageNamed:@"dingyue1_04.png"] forState:UIControlStateNormal];
    //[btn setTitle:theTitle forState:UIControlStateNormal];
    [self addSubview:btn];
    //[btn release];
    lblTitle=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, frame.size.width-10, frame.size.height)];
    [lblTitle setFont:[UIFont systemFontOfSize:13]];
    [lblTitle setText:theTitle];
    [lblTitle setTextColor:[UIColor blackColor]];
    [lblTitle setTextAlignment:UITextAlignmentCenter];
    [lblTitle setBackgroundColor:[UIColor clearColor]];
    [btn addSubview:lblTitle];
    [lblTitle release];
    return self;
}
-(void)btnTapped:(id)sender{
    if([url isEqualToString:@"myfocus"]){
        MyCoreData *mydata=[[MyCoreData alloc]initWithTableName:@"IndexData"];
        NSArray *arr=[mydata query];
        NSMutableArray *arrays=[[NSMutableArray alloc]init];
        for(int i=0;i<[arr count];i++){
            IndexData *obj=[arr objectAtIndex:i];
            NSMutableArray *ar=[NSMutableArray arrayWithCapacity:5];
            [ar addObject:[obj classid]];
            [ar addObject:[obj title]];
            [ar addObject:[obj titlepic]];
            [ar addObject:[obj bigpic]];
            [ar addObject:[obj isgood]];
            [ar addObject:[obj firsttitle]];
            [arrays addObject:ar];
        }
        //[arr release];
        [delegate itemSelected:arrays forIndex:index];
        [self myselected];
    }else{
        [delegate itemSelected:[NSMutableArray arrayWithObject:lblTitle.text] forIndex:index];
        [self myselected];
        //MyRequest *myrequest=[[[MyRequest alloc]init] autorelease];
        
        //[myrequest loaddataWithURL:url forObject:self];
    }
}
-(void)requestFinishedHandler:(ASIHTTPRequest*)request{
    if(request.responseStatusCode==200){
        //NSLog(@"%@",request.responseData);
        NSMutableArray *dict=[XMLParser parseChannelList:request.responseData];
        [delegate itemSelected:dict forIndex:index];
        [self myselected];
    }
}

-(void)myselected{
    [btn setImage:[UIImage imageNamed:@"dingyue1_08.jpg"] forState:UIControlStateNormal];
    [lblTitle setTextColor:[UIColor whiteColor]];
}
-(void)myunselected{
    [btn setImage:[UIImage imageNamed:@"dingyue1_04.png"] forState:UIControlStateNormal];
    [lblTitle setTextColor:[UIColor blackColor]];
}

-(void)dealloc{
    [btn release];
    delegate=nil;
    [super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
